package com.gotop.sms.fysms;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.app.util.DateTimeUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.pioneer.app.dev.osuser.user.Userlist;
import com.pioneer.app.dev.osuser.user.UserlistDAO;

public class BacterinReportAction extends ActionSupport {

	private Integer bacterinId;
	private String injectDate;
	private String orderDate;
	private String ystartDate;
	private String yendDate;
	private String userName;
	private List results;
	public List getResults() {
		return results;
	}

	public void setResults(List results) {
		this.results = results;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getYstartDate() {
		return ystartDate;
	}

	public void setYstartDate(String ystartDate) {
		this.ystartDate = ystartDate;
	}

	public String getYendDate() {
		return yendDate;
	}

	public void setYendDate(String yendDate) {
		this.yendDate = yendDate;
	}

	private String startDate;
	private String endDate;
	private List objects=null;
	
	private int total=0;
	
	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String notinjectSearch(){
		UserBacterinDAO dao=new UserBacterinDAO();
		String sql=null;
		if(null!=this.bacterinId){
			sql=" injectTime is null  and orderTime>'"+this.startDate+" 00:00:00"+"' and orderTime<'"+this.endDate+" 59:59:59' and bacterinId="+this.bacterinId;
		}else{
			sql="injectTime is null  and orderTime>'"+this.startDate+" 00:00:00"+"' and orderTime<'"+this.endDate+" 59:59:59' ";
		}
		this.objects=dao.findByCondition(sql);
//		this.objects=dao.findByCondition("injectTime is null  and bacterinId="+this.bacterinId);
		if(null!=this.objects){
			this.total=this.objects.size();
			UserBacterin ubvo=null;
			Bacterin bvo=null;
			BacterinDAO bdao=new BacterinDAO();
			UserlistDAO userDao=new UserlistDAO();
			String bName=null;
			Userlist user=null;
			for(int i=0;i<this.objects.size();i++){
				ubvo=(UserBacterin)this.objects.get(i);
				bvo=bdao.findById(ubvo.getBacterinId());
				if(null!=bvo){
					ubvo.setBacterinName(bvo.getName());
					ubvo.setType(bvo.getType());
					ubvo.setPrice(bvo.getPrice());
				}
				user=userDao.findById(ubvo.getUserId());
				if(null!=user){
					ubvo.setUserName(user.getCname());
				}
			}
		}
		return "list";
	}
	public String peopleSearch(){
		
		UserBacterinDAO dao=new UserBacterinDAO();
		String sql="from UserBacterin ub,Userlist ul where ub.userId=ul.id and ul.cname like '%"+this.userName+"%'";
		if(!this.startDate.equals("")){
			sql+=" and ul.birthday > '"+this.startDate+" 00:00:00'";
		}		
		if(!this.endDate.equals("")){
			sql+=" and ul.birthday < '"+this.endDate+" 59:59:59'";
		}
		if(!this.ystartDate.equals("")){
			sql+=" and ub.injectTime > '"+this.ystartDate+" 00:00:00'";
		}		
		if(!this.yendDate.equals("")){
			sql+=" and ub.injectTime < '"+this.yendDate+" 59:59:59'";
		}
		this.objects=dao.peopleSearch(sql);

		Bacterin bvo=null;
		BacterinDAO bdao=new BacterinDAO();
		if(null!=this.objects){
			this.total=this.objects.size();
			UserBacterin ub=null;
			Userlist ul=null;
			this.results=new ArrayList();
			for(int i=0;i<this.objects.size();i++){
				Object[] obj=(Object[])objects.get(i);
				ub=(UserBacterin)obj[0];
				ul=(Userlist)obj[1];
				bvo=bdao.findById(ub.getBacterinId());
				ub.setUserName(ul.getCname());
				ub.setSex(ul.getAgender());
				ub.setBrithday(ul.getBirthday());
				ub.setBacterinName(bvo.getName());
				this.results.add(ub);
			}
		}
		return "list";
	}
	public String injectSearch(){
		UserBacterinDAO dao=new UserBacterinDAO();
		String sql=null;
		if(null!=this.bacterinId){
			sql="injectTime>'"+this.startDate+" 00:00:00"+"' and injectTime<'"+this.endDate+" 59:59:59' and bacterinId="+this.bacterinId;
		}else{
			sql="injectTime>'"+this.startDate+" 00:00:00"+"' and injectTime<'"+this.endDate+" 59:59:59' ";
		}
		
		this.objects=dao.findByCondition(sql);
		if(null!=this.objects){
			this.total=this.objects.size();
			UserBacterin ubvo=null;
			Bacterin bvo=null;
			BacterinDAO bdao=new BacterinDAO();
			UserlistDAO userDao=new UserlistDAO();
			String bName=null;
			Userlist user=null;
			for(int i=0;i<this.objects.size();i++){
				ubvo=(UserBacterin)this.objects.get(i);
				bvo=bdao.findById(ubvo.getBacterinId());
				if(null!=bvo){
					ubvo.setBacterinName(bvo.getName());
					ubvo.setType(bvo.getType());
					ubvo.setPrice(bvo.getPrice());
				}
				user=userDao.findById(ubvo.getUserId());
				if(null!=user){
					ubvo.setUserName(user.getCname());
				}
			}
		}
		return "list";
	}
	public String orderSearch(){
		UserBacterinDAO dao=new UserBacterinDAO();
		String sql=null;
		if(null!=this.bacterinId){
			sql="orderTime>'"+this.startDate+" 00:00:00"+"' and orderTime<'"+this.endDate+" 59:59:59' and bacterinId="+this.bacterinId;
		}else{
			sql="orderTime>'"+this.startDate+" 00:00:00"+"' and orderTime<'"+this.endDate+" 59:59:59' ";
		}
		this.objects=dao.findByCondition(sql);
		if(null!=this.objects){
			this.total=this.objects.size();
			UserBacterin ubvo=null;
			Bacterin bvo=null;
			BacterinDAO bdao=new BacterinDAO();
			UserlistDAO userDao=new UserlistDAO();
			String bName=null;
			Userlist user=null;
			for(int i=0;i<this.objects.size();i++){
				ubvo=(UserBacterin)this.objects.get(i);
				bvo=bdao.findById(ubvo.getBacterinId());
				if(null!=bvo){
					ubvo.setBacterinName(bvo.getName());
					ubvo.setType(bvo.getType());
					ubvo.setPrice(bvo.getPrice());
				}
				user=userDao.findById(ubvo.getUserId());
				if(null!=user){
					ubvo.setUserName(user.getCname());
				}
			}
		}
		return "list";
	}

	public Integer getBacterinId() {
		return bacterinId;
	}

	public void setBacterinId(Integer bacterinId) {
		this.bacterinId = bacterinId;
	}

	public String getInjectDate() {
		return injectDate;
	}

	public void setInjectDate(String injectDate) {
		this.injectDate = injectDate;
	}

	public String getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}


	public List getObjects() {
		return objects;
	}

	public void setObjects(List objects) {
		this.objects = objects;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	
}
